/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* 
 * File:   serialization.h
 * Author: picci
 *
 * Created on July 29, 2011, 6:26 PM
 */

#ifndef SERIALIZATION_H
#define	SERIALIZATION_H

#include "prj_definitions.h"
#include "pipe_meta.h"

#include <string>

#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/util/XMLException.hpp>

namespace NAMESPACE {

// ****** PipeStepXMLSerializer definition *************************************
    
class PipeStepXMLSerializer : public AbsPipeStepDescrConsumer{
public:
    const static std::string NS;
    const static std::string PIPELINES_COLLECTION;
    const static std::string PIPELINE;
    const static std::string PIPESTEP;
    const static std::string GENERATORS;
    const static std::string GENERATOR;
    const static std::string IN_GENERATORS;
    const static std::string OUT_GENERATORS;
    const static std::string CRITERIA;
    const static std::string CRITERION;
    const static std::string FORMATTERS;
    const static std::string PARAMETER;
    
    ~PipeStepXMLSerializer();
    
    void consume(const PipeStepDescriptor& d);    
    bool serialize(const std::string& filename);
    
    static PipeStepXMLSerializer* get_instance(const std::string& s = "LS");
    static void free_instance();
private:
    PipeStepXMLSerializer();
    PipeStepXMLSerializer(const PipeStepXMLSerializer& psx);
    PipeStepXMLSerializer(const std::string& s);
    PipeStepXMLSerializer& operator=(const PipeStepXMLSerializer& psx);
    
    void consume_descriptor(const PipeStepDescriptor& d, xercesc::DOMElement* root);
    
    static PipeStepXMLSerializer* instance;
    
    xercesc::DOMImplementation* impl;
    xercesc::DOMDocument* doc;
};

// -----------------------------------------------------------------------------


// ****** XMLPipeStepDescriptorBuilder definition ******************************

class XMLPipeStepDescriptorBuilder : public AbsPipeStepDescrBuilder{
public:
    ~XMLPipeStepDescriptorBuilder();
    
    virtual void build(std::vector<PipeStepDescriptor*>&);
    
    static XMLPipeStepDescriptorBuilder* get_instance(const std::string& _filename, const std::string& s = "LS");
    static void free_instance();
private:
    XMLPipeStepDescriptorBuilder();
    XMLPipeStepDescriptorBuilder(const XMLPipeStepDescriptorBuilder&);
    XMLPipeStepDescriptorBuilder(const std::string& _filename, const std::string& s);
    XMLPipeStepDescriptorBuilder& operator= (const XMLPipeStepDescriptorBuilder&);
    
    void configuration_from_DOM(xercesc::DOMNodeList* params, int idx, Configuration& cfg);
    void parse_generators(xercesc::DOMElement* elem, SingleStepDescriptor& ssd);
    
    static XMLPipeStepDescriptorBuilder* instance;
    
    xercesc::DOMImplementation* impl;
    xercesc::DOMLSParser* parser;
    
    std::string filename;
};

// -----------------------------------------------------------------------------

}

#endif	/* SERIALIZATION_H */

